/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import connection.DaoTransaction;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.export.JRPdfExporter;

/**
 *
 * @author Student
 */
public class GetTransactionList extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) {
        ServletOutputStream stream = null;

        try {
            JRDataSource ds = new JRResultSetDataSource(new DaoTransaction().getToysRs());

            File pdf = new File("Q:\\list.pdf");

            JasperPrint printer = JasperFillManager.fillReport("C:\\ELITE 7\\Workspace\\CaseStudy\\src\\java\\reports\\TransactionList.jasper", null, ds);


            JRExporter exporter = new JRPdfExporter();

            exporter.setParameter(JRExporterParameter.OUTPUT_FILE, pdf);

            exporter.setParameter(JRExporterParameter.JASPER_PRINT, printer);
            exporter.exportReport();

            response.setContentType("application/pdf");
            stream = response.getOutputStream();
            response.setContentLength((int) pdf.length());

            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(pdf));


            int readBytes = 0;

            while ((readBytes = bis.read()) != -1) {
                stream.write(readBytes);
            }

        } catch (IOException ex) {
            Logger.getLogger(GetTransactionList.class.getName()).log(Level.SEVERE, null, ex);
        } catch (JRException ex) {
            Logger.getLogger(GetTransactionList.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                stream.close();
            } catch (IOException ex) {
                Logger.getLogger(GetTransactionList.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
